package com.cade.agriculturemall.view.personalCenter.collect;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.model.GoodsDetailEntity;
import com.cade.agriculturemall.model.ItemFavouriteEntity;
import com.cade.agriculturemall.view.classify.goodsdetail.GoodsDetailActivity;
import com.cade.agriculturemall.widget.EmptyLayout;

import java.util.List;

public class CollectActivity extends BaseActivity<CollectContract.IPresenter> implements CollectContract.IView {
    private List<ItemFavouriteEntity> entity;
    private SwipeMenuListView mListView;
    private EmptyLayout mEmptyLayout;
    private CollectAdapter mAdapter = new CollectAdapter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        setupToolbar(true);
        mListView = (SwipeMenuListView) findViewById(R.id.collect_listView);
        mEmptyLayout= (EmptyLayout) findViewById(R.id.collect_emptyLayout);
        if (mAdapter != null) {
            mListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
            mListView.setAdapter(mAdapter);
        }
        mListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);

        // set creator
        mListView.setMenuCreator(creator);
        // step 2. listener item click event
        mListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                ItemFavouriteEntity item = entity.get(position);
                switch (index) {
                    case 0:
                        // open
//                        open(item);
                        GoodsDetailEntity goodEntity=entity.get(position).getProId();
                        mPresenter.transformIntoShopCar(goodEntity);
                        mPresenter.delete(entity.get(position).getObjectId());
                        entity.remove(entity.get(position));
                        mAdapter.notifyDataSetChanged();
                        break;
                    case 1:
                        // delete
//					delete(item);
                        Toast.makeText(mActivity, position + "", Toast.LENGTH_SHORT).show();
                        mPresenter.delete(entity.get(position).getObjectId());
//                        mAdapter.setList(entity);
                        entity.remove(entity.get(position));
                        mAdapter.notifyDataSetChanged();
                        break;
                }
                return false;
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(),GoodsDetailActivity.class);
                intent.putExtra("objectId", entity.get(position).getProId().getObjectId());
                startActivity(intent);
            }
        });
        mEmptyLayout.setOnEmptyLayoutListener(new EmptyLayout.OnEmptyLayoutListener() {
            @Override
            public boolean onReloadClick(EmptyLayout layout) {
                mPresenter.loadProducter();
                return true;
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        mEmptyLayout.loading();
        mPresenter.loadProducter();
    }

    @Override
    public void setupView() {

    }

    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {
        mPresenter.loadProducter();
    }

    @Override
    public CollectContract.IPresenter setupPresenter() {
        return new CollectPresenter(this);
    }

    @Override
    public void receiveList(List<ItemFavouriteEntity> list) {
        if (list.size()!=0) {
            mEmptyLayout.reset();
            entity = list;
            mAdapter.setList(entity);
            mAdapter.notifyDataSetChanged();
        }else {
            mEmptyLayout.empty();
        }
    }

    SwipeMenuCreator creator = new SwipeMenuCreator() {

        @Override
        public void create(SwipeMenu menu) {
            // create "open" item
            SwipeMenuItem openItem = new SwipeMenuItem(
                    getApplicationContext());
            // set item background
            openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                    0xCE)));
            // set item width
            openItem.setWidth(dp2px(90));
            // set item title
//            openItem.setTitle("Open");
            openItem.setIcon(R.mipmap.shopping_cart);
            // set item title fontsize
//            openItem.setTitleSize(18);
            // set item title font color
            openItem.setTitleColor(Color.WHITE);
            // add to menu
            menu.addMenuItem(openItem);

            // create "delete" item
            SwipeMenuItem deleteItem = new SwipeMenuItem(
                    getApplicationContext());
            // set item background
            deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                    0x3F, 0x25)));
            // set item width
            deleteItem.setWidth(dp2px(90));
            // set a icon
            deleteItem.setIcon(R.mipmap.delete);
            // add to menu
            menu.addMenuItem(deleteItem);
        }
    };

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }
}
